package com.example.tutorial.arrayadapter;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.tutorial.model.Task;
import com.example.tutorial.mytodolist.R;
import com.example.tutorial.ultils.DateUltil;

public class TaskArrayAdapter extends BaseAdapter {
	private Activity activity;
	private ArrayList<Task> data;
	private static LayoutInflater inflater = null;

	public TaskArrayAdapter(Activity a, ArrayList<Task> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.list_row_task, parent, false);
		Task task = data.get(position);
		if (task != null) {
			TextView text = (TextView) vi.findViewById(R.id.title);
			text.setText(task.getTitle());
			TextView duration = (TextView) vi.findViewById(R.id.duration);
			StringBuilder sb = new StringBuilder();
			sb.append(task.getNotifyDate().getDate());
			sb.append("/");
			sb.append(task.getNotifyDate().getMonth());
			sb.append("/");
			sb.append(task.getNotifyDate().getYear());
			duration.setText(sb);
			SimpleDateFormat sm = new SimpleDateFormat("dd-MM-yyyy");
			int d;
			try {
				d = DateUltil.compare2Date(task.getNotifyDate(),
						sm.parse(Calendar.getInstance().toString()));

				if (d == -1) {

					text.setPaintFlags(text.getPaintFlags()
							| Paint.STRIKE_THRU_TEXT_FLAG);
					duration.setPaintFlags(duration.getPaintFlags()
							| Paint.STRIKE_THRU_TEXT_FLAG);
				} else {
					text.setPaintFlags(text.getPaintFlags()
							& Paint.STRIKE_THRU_TEXT_FLAG);
					duration.setPaintFlags(duration.getPaintFlags()
							& Paint.STRIKE_THRU_TEXT_FLAG);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return vi;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}
